﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Management;
using System.Xml.Linq;
namespace Twi.COMMON.WPF
{
    /// <summary>
    /// 获取客户端计算机信息
    /// </summary>
    public class ComputInfo
    {
        /// <summary>
        /// 获取主机名
        /// </summary>
        /// <returns></returns>
        public static string HostName
        {
            get
            {
                string hostname = Dns.GetHostName();
                return hostname;
            }
        }
        public static string GetIP()
        {
            string ipAddress = string.Empty;
            try
            {
                IPAddress[] addressList = Dns.GetHostEntry(HostName).AddressList;
                for (int i = 0; i < addressList.Length; i++)
                {
                    if (!addressList[i].IsIPv6LinkLocal && !addressList[i].IsIPv6Teredo)
                    {
                        ipAddress = addressList[i].ToString();
                    }
                }
            }
            catch { }
            return ipAddress;
        }
        /// <summary>
        /// 获取IP地址
        /// </summary>
        /// <returns></returns>
        public static List<string> GetIPList()
        {
            List<string> ipList = new List<string>();
            try
            {
                IPAddress[] addressList = Dns.GetHostEntry(HostName).AddressList;
                for (int i = 0; i < addressList.Length; i++)
                {
                    ipList.Add(addressList[i].ToString());
                }
            }
            catch { }
            return ipList;
        }

        public static XElement GetIPXElement()
        {
            XElement xeIPList = new XElement("IPList");
            try
            {
                IPAddress[] addressList = Dns.GetHostEntry(HostName).AddressList;
                for (int i = 0; i < addressList.Length; i++)
                {
                    xeIPList.Add(new XElement("IP", addressList[i].ToString()));
                }
            }
            catch { }
            return xeIPList;
        }

        /// <summary>
        /// 获取Mac地址
        /// </summary>
        /// <returns></returns>
        public static List<string> GetMacList()
        {
            List<string> macList = new List<string>();
            try
            {
                ManagementClass mc;
                mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if (mo["IPEnabled"].ToString() == "True")
                        macList.Add(mo["MacAddress"].ToString());
                }
            }
            catch { }
            return macList;
        }

        public static XElement GetMacXElement()
        {
            XElement xeMacList = new XElement("MACList");
            try
            {
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if (mo["IPEnabled"].ToString() == "True")
                        xeMacList.Add(new XElement("MAC", mo["MacAddress"].ToString()));
                }
            }
            catch { }
            return xeMacList;
        }

        //
        /// <summary>
        /// 获取操作系统的登录用户名
        /// </summary>
        /// <returns></returns>
        public static string GetLogonName()
        {
            string user_name = ""; //登录用户名
            try
            {
                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    user_name = mo["UserName"].ToString();
                }
            }
            catch { }
            return user_name;
        }
        /// <summary>
        /// 获取操作系统类型
        /// </summary>
        /// <returns></returns>
        public static string GetSystemType()
        {
            string system_type = ""; //PC类型
            try
            {
                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    system_type = mo["SystemType"].ToString();
                }
            }
            catch { }
            return system_type;
        }

        /// <summary>
        /// 获取内存信息
        /// </summary>
        /// <returns></returns>
        public static string GetTotalPhysicalMemory()
        {
            string memory = ""; //物理内存
            try
            {
                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    memory = mo["TotalPhysicalMemory"].ToString();
                }
            }
            catch { }
            return memory;
        }




    }
}
